<template>
	<view class="container">
		<view class="avatar-wrapper">
			<image class="avatar" src="/static/head_icon.png"></image>
		</view>
		<view class="form-wrapper">
			<uni-forms ref="form" :rules="{}">
				<uni-forms-item label="用户名" name="username">
					<uni-easyinput v-model="user.name" placeholder="请输入用户名" />
				</uni-forms-item>
				<uni-forms-item label="密码" name="password">
					<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
				</uni-forms-item>
				<uni-forms-item>
					<button type="primary" @click="login">登录</button>
				</uni-forms-item>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					name: '',
					password: ''
				}
			}
		},
		methods: {
			login() {
				uni.request({
					url: 'http://localhost:8090/freshmenapp/user/login',
					method: 'POST',
					data: this.user,
					success: res => {
						if (res.data.success) {
							uni.setStorageSync('userinfo', res.data.data)
							uni.showToast({
								title: '' + res.data.data.username,
								icon: 'none'
							});
							uni.switchTab({
								url: '/pages/profile/profile'
							})
						} else {
							uni.setStorageSync('userinfo', null)
							uni.showToast({
								title: '失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '请求失败',
							icon: 'none'
						})
					}
				})
			}

		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding-top: 20px;
	}

	.avatar-wrapper {
		margin-bottom: 40rpx;
	}

	.avatar {
		width: 200rpx;
		height: 200rpx;
		border-radius: 50%;
	}

	.form-wrapper {
		width: 80%;
	}

	uni-forms-item {
		margin-bottom: 20px;
	}

	button {
		background: linear-gradient(to right, #0167ff, #418aff);
	}
</style>